//
//  DZReaderViewController.m
//  好奇心日报
//
//  Created by 董筱泽 on 2017/11/26.
//  Copyright © 2017年 董筱泽. All rights reserved.
//

#import "DZReaderViewController.h"

//使用WKWebView需先导入WebKit框架
#import <WebKit/WebKit.h>
#import "JFConfigFile.h"
#import "Masonry.h"
#import "MBProgressHUD+JFProgressHUD.h"



@interface DZReaderViewController ()<WKNavigationDelegate>

/** 加载动画view*/
@property (nonatomic, strong) UIView *loadingView;
@property (nonatomic, strong) UIImageView *loadingImageView;

@end

@implementation DZReaderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
    
}
- (void)loadView {
    [super loadView];
    
    WKWebView *readerWebView = [[WKWebView alloc] initWithFrame:self.view.bounds];
    readerWebView.navigationDelegate = self;
    [readerWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:_newsUrl] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:7.0]];
    [self.view addSubview:readerWebView];
    [self.view addSubview:self.loadingView];
    [self.loadingView addSubview:self.loadingImageView];
    [self setupUI];
    
}
    /// 懒加载，加载动画界面
- (UIView *)loadingView {
    if (!_loadingView) {
        _loadingView = [[UIView alloc] initWithFrame:self.view.bounds];
        _loadingView.backgroundColor = [UIColor whiteColor];
    }
    return _loadingView;
}
    
    ///懒加载，加载动画imageview
- (UIImageView *)loadingImageView {
    if (!_loadingImageView) {
        _loadingImageView = [[UIImageView alloc] init];
        NSMutableArray *imageMutableArray = [[NSMutableArray alloc] init];
        for (int i = 0; i < 93; i ++) {
            NSString *imageName = [NSString stringWithFormat:@"QDArticleLoading_0%d",i];
            UIImage *image = [UIImage imageNamed:imageName];
            [imageMutableArray addObject:image];
        }
        _loadingImageView.animationImages = imageMutableArray;
        _loadingImageView.animationDuration = 3.0;
        _loadingImageView.animationRepeatCount = MAXFLOAT;
    }
    return _loadingImageView;
}
    
    /// 使用Masonry，自动布局子控件
- (void)setupUI {
    [self.loadingImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.offset(100);
        make.height.offset(100);
        make.centerX.equalTo(self.view.mas_centerX);
        make.centerY.equalTo(self.view.mas_centerY);
    }];
}
- (void)setNewsUrl:(NSString *)newsUrl {
    _newsUrl = newsUrl;
}
    
    
    
#pragma mark --- WKNavigationDelegate
    
    /// WXWebView开始加载时调用
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
    [self.loadingImageView startAnimating];
}
    
    /// WXWebView加载完成时调用
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
    //渐隐加载动画
    [UIView animateWithDuration:0.3
                     animations:^{
                         [self.loadingView setAlpha:0];
                     } completion:^(BOOL finished) {
                         [self.loadingImageView stopAnimating];
                         [self.loadingView removeFromSuperview];
                         self.loadingView = nil;
                     }];
}
    
    /// WXWebView加载失败时调用
- (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error {
    [self.loadingImageView stopAnimating];
    [MBProgressHUD promptHudWithShowHUDAddedTo:self.view message:@"加载失败，请检查网络"];
    [NSThread sleepForTimeInterval:1.3];
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
